package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回背包物品更新消息
 */
public class Backpack_GoodsChangeReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 16003; }

    public Integer BackpackItemChangeCount; //更新的种类数量
    public ArrayList<ChangeItem> ItemList; //更改项

    /**
     * 更改项
     */
    public static class ChangeItem
    {
        public Integer BackpackId; //背包项编号
        public byte ChangeType; //改变类型
        public byte GoodsType; //物品类型
        public Integer GoodsId; //物品编号
        public Integer GoodsCount; //物品数量
        public Integer GoodsServerId; //物品服务器端Id
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(BackpackItemChangeCount);
            for (int i = 0; i < BackpackItemChangeCount; i++) {
                dataStream.WriteInt(ItemList.get(i).BackpackId);
                dataStream.WriteByte(ItemList.get(i).ChangeType);
                dataStream.WriteByte(ItemList.get(i).GoodsType);
                dataStream.WriteInt(ItemList.get(i).GoodsId);
                dataStream.WriteInt(ItemList.get(i).GoodsCount);
                dataStream.WriteInt(ItemList.get(i).GoodsServerId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static Backpack_GoodsChangeReturnProto GetProto(byte[] buffer) {
        Backpack_GoodsChangeReturnProto proto = new Backpack_GoodsChangeReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.BackpackItemChangeCount = dataStream.ReadInt();
            proto.ItemList = new ArrayList<ChangeItem>();
            for (int i = 0; i < proto.BackpackItemChangeCount; i++) {
                ChangeItem _Item = new ChangeItem();
                _Item.BackpackId = dataStream.ReadInt();
                _Item.ChangeType = (byte)dataStream.ReadByte();
                _Item.GoodsType = (byte)dataStream.ReadByte();
                _Item.GoodsId = dataStream.ReadInt();
                _Item.GoodsCount = dataStream.ReadInt();
                _Item.GoodsServerId = dataStream.ReadInt();
                proto.ItemList.add(_Item);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}